package com.zelin.manager.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zelin.entity.BrandEntity;
import com.zelin.entity.vo.BrandVoEntity;
import com.zelin.sellergoods.service.BrandService;
import com.zelin.utils.PageUtils;
import com.zelin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * @author WF
 * @email 2568783935@qq.com
 * @date 2021-07-23 21:10:35
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Reference(timeout = 5000)
    private BrandService brandService;
    /**
     * 列表cd
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);
        System.out.println("page = " + page.getList());
        return R.ok().put("page", page);
    }
    /**
     * 显示所有品牌
     */
    @RequestMapping("/findAll")
    public R findAll(){
        //1. 查询所有品牌
        List<BrandEntity> list = brandService.list();
        //2. 将当前页集合中的对象更改为BrandVoEntity对象，因为这个对象是模板表中的brandIds[{id:1,text:'aa'}]中某一条记录
        List<BrandVoEntity> collect = list.stream()
                                          .map(m -> new BrandVoEntity(m.getId(), m.getName()))
                                          .collect(Collectors.toList());
        //3. 返回结果
        return R.ok().put("list", collect);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BrandEntity brand){
        System.out.println("brand = " + brand);
		brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
